package thread;
/**
 * sleep方法要求我们必须处理中断异常。
 * 当一个线程调用sleep方法处于睡眠阻塞的过程中如果此时该线程的interrupt方法被调用，那么
 * 此时该线程的睡眠阻塞状态会被中断，sleep方法就会抛出中断异常
 */
public class SleepDemo2 {
    public static void main(String[] args) {
        Thread p = new Thread("派大星"){
            public void run(){
                System.out.println(getName()+":在睡觉！");
                try {
                    Thread.sleep(50000000);
                } catch (InterruptedException e) {
                    System.out.println("海绵宝宝，我在睡觉");
                }
                System.out.println("我起来了");
            }
        };
        Thread h = new Thread("海绵宝宝"){
            public void run(){
                System.out.println(getName()+"派大星");
                for (int i = 0; i < 5; i++) {
                    System.out.println("快醒醒");
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                p.interrupt();
            }
        };

        p.start();
        h.start();
    }
}
